/*
 * @Author: jiadongjin 1247452156@qq.com
 * @Date: 2024-11-19 14:50:05
 * @LastEditors: 郝奔 1425466221@qq.com
 * @LastEditTime: 2025-01-06 17:41:32
 * @Description: 
 * Copyright (c) 2024-present HBIS Digital Technology Co.,Ltd. All rights reserved. 
 * @FilePath: /HG-dqbasic_open_platform/src/framework/unified-navigation/utils/index.ts
 */
/**
 * Created by PanJiaChen on 16/11/18.
 */
import { getToken } from '@/framework/utils/auth'
import router from '@/router';
/**
 * 如果是本应用的就在应用内通过router.push跳转；其他应用再打开新窗口
 * @param {string} url
 * @returns {Object}
 */
export function openOrPushUrl(item: any, path: string, closeGrid?: () => void): void {
  if (path) {
    const index = path.indexOf(location.origin)
    if (index > -1) {
      handleSameOrigin(item, path, closeGrid)
    } else {
      handleDifferentOrigin(item, path)
    }
  }
}
/**
 * @description: 本应用的就在应用内通过router.push跳转
 * @param {any} item
 * @param {string} path
 * @return {*}
 */
function handleSameOrigin(item: any, path: string, closeGrid?: () => void): void {
  if (item.appSource == 3) {
    const { appSource, appCode, appId, appName, menuResourceId, defaultMenuCode, defaultMenuName, menuCode, menuName, formId, antdvComponent, antdvLinkOpenType } = item
    router.push({
      path: '/home/visitorTemplate',
      query: {
        appSource,
        appCode,
        menuId: menuResourceId,
        menuCode: defaultMenuCode ? defaultMenuCode : menuCode,
        appName,
        appId,
        menuName: defaultMenuName ? defaultMenuName : menuName,
        formId,
        antdvComponent,
        antdvLinkOpenType
      }
    })
  } else {
    router.push(item.menuPath || item.defaultMenuPath)
    closeGrid && closeGrid()
  }
}
/**
 * @description: 其他应用再打开新窗口
 * @param {any} item
 * @param {string} path
 * @return {*}
 */
function handleDifferentOrigin(item: any, path: string): void {
  if (item.appSource === 3) {
    window.open(`${item.appAddress}/home/visitorTemplate?token=${getToken()}&appSource=${item.appSource}&appCode=${item.appCode}&appName=${item.appName}&appId=${item.appId}&formId=${item.formId}&menuId=${item.menuResourceId}&menuCode=${item.defaultMenuCode ? item.defaultMenuCode : item.menuCode}&menuName=${item.defaultMenuName ? item.defaultMenuName : item.menuName}&antdvComponent=${item.antdvComponent}&antdvLinkOpenType=${item.antdvLinkOpenType}`)
  } else {
    const queryString = item.integratedAppType === 1 ? '' : (path.includes('?') ? `&token=${getToken()}` : `?token=${getToken()}`);
    window.open(`${path}${queryString}`)
    // window.open(`${path}?token=${getToken()}`)
  }
}
// export const specialData = ['WeShyperData', 'WeShyperAIData']